package cn.tedu.micro.demo.client.dlx;

import cn.tedu.micro.demo.client.RabbitChannelManager;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 测试声明死信队列结构
 */
public class DlxDemo {
    //声明一个可以处理业务队列产生死信的死信队列结构
    /**
     * 参考约定名称和绑定关系 声明一套死信的组件结构
     */
    private static final String BIZ_EX="biz_ex";
    private static final String BIZ_Q="biz_q";
    private static final String DLX_EX="dlx_ex";
    private static final String DLX_Q="dlx_q";
    private static final String DLX_RK="dlx_rk";
    @Test
    public void dlxDeclare() throws IOException {
        //1.获取连接
        Channel channel = RabbitChannelManager.newChannel();
        //2.声明死信交换机 死信队列 绑定关系
        channel.exchangeDeclare(DLX_EX, BuiltinExchangeType.DIRECT);
        channel.queueDeclare(DLX_Q, false, false, false, null);
        channel.queueBind(DLX_Q, DLX_EX, DLX_RK);
        //3.声明业务交换机 业务队列 业务队列设置死信的数据属性
        channel.exchangeDeclare(BIZ_EX, BuiltinExchangeType.FANOUT);
        //3.1声明业务队列,只要队列出现死信,就要携带死信路由进入死信交换机
        Map<String,Object> args= new HashMap<>();
        args.put("x-dead-letter-exchange",DLX_EX);
        args.put("x-dead-letter-routing-key",DLX_RK);
        channel.queueDeclare(BIZ_Q, false, false, false, args);
        channel.queueBind(BIZ_Q, BIZ_EX, "");
    }
}
